package com.vito.oa.email.impl;

import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.oa.email.model.OaEmailContant;
import com.vito.oa.email.service.IEmailMbeanService;

@Service
public class EmailMbeanServiceImp implements IEmailMbeanService {
	 
	private MBeanServerConnection jmxServer;

	private void init(String[] user, String JMXServiceURL) throws Exception {
		Map<String, String[]> conntion = new HashMap<String, String[]>();
		conntion.put("jmx.remote.credentials", user);
		JMXServiceURL serviceURL = new JMXServiceURL(JMXServiceURL);
		JMXConnector jmxc = JMXConnectorFactory.connect(serviceURL, conntion);
		jmxServer = jmxc.getMBeanServerConnection();
	}

	public int createEmailBox(String nameSpace, String userName, String mailBoxName, String[] user, String JMXServiceURL) {
		try {
			init(user, JMXServiceURL);
		} catch (Exception e1) {
			return OaEmailContant.CONNECT_FAILED;
		}
		try {
			ObjectName MbeanName = new ObjectName("org.apache.james:type=component,name=mailboxmanagerbean");
			String operationName = "createMailbox";
			Object[] params = { nameSpace, userName, mailBoxName };
			String[] signature = { String.class.getName(), String.class.getName(), String.class.getName() };
			jmxServer.invoke(MbeanName, operationName, params, signature);

			return OaEmailContant.SUCCESS;
		} catch (Exception e) {
			return OaEmailContant.ALREADY_EXIST;
		}
	}

	public int addUser(String userName, String password, String[] user, String JMXServiceURL) {
		try {
			init(user, JMXServiceURL);
		} catch (Exception e1) {
			return OaEmailContant.CONNECT_FAILED;
		}

		try {
			ObjectName MbeanName = new ObjectName("org.apache.james:type=component,name=usersrepository");
			String operationName = "addUser";
			Object[] params = { userName, password };
			String[] signature = { String.class.getName(), String.class.getName() };
			jmxServer.invoke(MbeanName, operationName, params, signature);
			return OaEmailContant.SUCCESS;
		} catch (Exception e) {
			return OaEmailContant.ALREADY_EXIST;
		}
	}

	public int deleteMailBox(String nameSpace, String userName, String mailBoxName, String[] user, String JMXServiceURL) {
		try {
			init(user, JMXServiceURL);
		} catch (Exception e1) {
			return OaEmailContant.CONNECT_FAILED;
		}
		try {
			ObjectName MbeanName = new ObjectName("org.apache.james:type=component,name=mailboxmanagerbean");
			String operationName = "deleteMailbox";
			Object[] params = { userName, userName, mailBoxName };
			String[] signature = { String.class.getName(), String.class.getName(), String.class.getName() };
			jmxServer.invoke(MbeanName, operationName, params, signature);
			return OaEmailContant.SUCCESS;
		} catch (Exception e) {
			return OaEmailContant.ALREADY_EXIST;
		}
	}
}
